package com.sky.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.Result.PageResult;
import com.sky.constant.MessageConstant;
import com.sky.exception.CustomException;
import com.sky.mapper.ShopMapper;
import com.sky.model.dto.ShopDTO;
import com.sky.model.dto.ShopPageQueryDTO;
import com.sky.model.entity.Shop;
import com.sky.model.vo.ShopVO;
import com.sky.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;

    /**
     * 商品分页查询
     * @param shopPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(ShopPageQueryDTO shopPageQueryDTO) {
        // 确保页码和每页条数是合法的
        int page = Math.max(shopPageQueryDTO.getPage(), 1);  // 页码不能小于 1
        int pageSize = Math.max(shopPageQueryDTO.getPageSize(), 1);  // 每页条数不能小于 1
        // 计算分页的偏移量和限制条数
        int offset = (page - 1) * pageSize;  // 当前页的偏移量
        int limit = pageSize;  // 每页的条数
        // 查询数据
        List<ShopVO> shopList = shopMapper.pageQuery(shopPageQueryDTO.getName(),shopPageQueryDTO.getCategoryId(),shopPageQueryDTO.getStatus(), offset, limit);
        // 获取总记录数
        long total = shopMapper.countShops(shopPageQueryDTO);
        // 返回分页结果
        return new PageResult(total, shopList);
    }


    /**
     * 新增商品
     * @param shopDTO
     */
    @Override
    public void save(ShopDTO shopDTO) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDTO, shop);
        shop.setCreateTime(LocalDateTime.now());
        shop.setUpdateTime(LocalDateTime.now());
        shop.setCategoryName(shopMapper.getCategoryName(shop.getCategoryId()));
        //向shop表插入1条数据
        shopMapper.insert(shop);
    }

    /**
     * 启用禁用商品
     * @param status
     * @param id
     */
    @Override
    public void startOrstop(Integer status, Long id) {
        Shop shop = new Shop();
        shop.setId(id);
        shop.setStatus(status);
        shop.setUpdateTime(LocalDateTime.now());
        shopMapper.update(shop);
    }

    /**
     * 批量删除商品
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        if (ids.isEmpty()) {
            throw new CustomException(MessageConstant.ID_LIST_EMPTY);
        }
        //判断当前商品是否能够删除-是否存在起售的商品?
        for (Long id : ids) {
            Shop shop = shopMapper.getById(id);
            if (shop.getStatus() == MessageConstant.ENABLE) {
                //当前商品处于起售当中
                throw new CustomException(MessageConstant.SHOP_ON_SALE);
            }
        }
        for (Long id : ids) {
            shopMapper.deleteById(id);
        }
    }

    /**
     * 批量启用禁用商品
     * @param ids
     */
    @Override
    @Transactional
    public void startOrstops(List<Long> ids) {
            if (ids.isEmpty()) {
                throw new CustomException(MessageConstant.ID_LIST_EMPTY);
            }

            // 获取第一个状态的值
            Long firstId = ids.get(0);
            Shop firstShop = shopMapper.getById(firstId);
            Integer firstStatus = firstShop.getStatus();

            // 比对其它值，若不一致，则抛出错误
            for (Long id : ids) {
                Shop shop = shopMapper.getById(id);
                if (!shop.getStatus().equals(firstStatus)) {
                    throw new CustomException(MessageConstant.STATUS_MISMATCH);
                }
            }

        // 确定新的状态值，0为禁用，1为启用
        Integer newStatus = (firstStatus == 1) ? 0 : 1;

        // 更新所有商品的状态
        for (Long id : ids) {
            Shop shop = new Shop();
            shop.setId(id);
            shop.setStatus(newStatus);
            shop.setUpdateTime(LocalDateTime.now());

            shopMapper.update(shop);
        }
    }

    /**
     * 修改商品
     * @param shopDTO
     */
    @Override
    public void update(ShopDTO shopDTO) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDTO, shop);
        shop.setUpdateTime(LocalDateTime.now());

        shopMapper.update(shop);
    }

    /**
     * 用户商品主页+查询
     * @param shopPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(ShopPageQueryDTO shopPageQueryDTO) {
        PageHelper.startPage(shopPageQueryDTO.getPage(), shopPageQueryDTO.getPageSize());
        Page<ShopVO> page = shopMapper.page(shopPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
}

